package dao

import (
	"context"
	"device-admin/internal/dal"
	"device-admin/internal/models"
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type MaterialTheme struct{}

func (MaterialTheme) Create(ctx context.Context, db *gorm.DB, merchantID, userName, name, soundSource string, permanent int, startTime, endTime int64, personalityPrompt string) (ID string, err error) {
	m := dal.Use(db).MaterialTheme

	var d models.MaterialTheme
	d.ID, _ = helper.UUID{}.String()
	d.MerchantID = merchantID
	d.Name = name
	d.SoundSource = soundSource
	d.Permanent = permanent
	d.StartTime = startTime
	d.EndTime = endTime
	d.PersonalityPrompt = personalityPrompt
	d.CreatedUser = userName
	m.WithContext(ctx).Create(&d)
	return d.ID, nil
}

func (MaterialTheme) Update(ctx context.Context, db *gorm.DB, ID, name, soundSource string, permanent int, startTime, endTime int64, personalityPrompt string) (err error) {
	if ID == "" {
		return
	}
	m := dal.Use(db).MaterialTheme
	m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Where(m.DeletedAt.Eq(0)).
		Select(m.Name, m.SoundSource, m.Permanent, m.StartTime, m.EndTime, m.PersonalityPrompt).
		Updates(models.MaterialTheme{
			Name:              name,
			SoundSource:       soundSource,
			Permanent:         permanent,
			StartTime:         startTime,
			EndTime:           endTime,
			PersonalityPrompt: personalityPrompt,
		})
	return nil
}

func (MaterialTheme) Detail(ctx context.Context, db *gorm.DB, merchantID, ID, name string) (res *models.MaterialTheme, count int64, err error) {
	m := dal.Use(db).MaterialTheme
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	if name != "" {
		tx = tx.Where(m.Name.Eq(name))
	}
	res, _ = tx.Take()
	count, err = tx.Count()
	return
}

func (MaterialTheme) List(ctx context.Context, db *gorm.DB, merchantID, ID, name string, page, limit int) (res []*models.MaterialTheme, count int64, err error) {
	m := dal.Use(db).MaterialTheme
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	if name != "" {
		tx = tx.Where(m.Name.Eq(name))
	}
	res, count, err = tx.FindByPage(GetOffsetLimit(page, limit))
	return
}

func (MaterialTheme) SameNameExist(ctx context.Context, db *gorm.DB, merchantID, name string, exceptID string) (count int64, err error) {
	m := dal.Use(db).MaterialTheme
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if name != "" {
		tx = tx.Where(m.Name.Eq(name))
	}
	if exceptID != "" {
		tx = tx.Where(m.ID.Neq(exceptID))
	}
	count, err = tx.Count()
	return
}

func (MaterialTheme) Delete(ctx context.Context, db *gorm.DB, ID string) (err error) {
	if ID == "" {
		return
	}
	m := dal.Use(db).MaterialTheme
	m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0)).
		Where(m.ID.Eq(ID)).
		UpdateColumn(m.DeletedAt, time.Now().Unix())
	return
}
